package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.CleanMapper;
import cn.group.hotel2.mapper.OrderMapper;
import cn.group.hotel2.pojo.dto.CleanAddNewDTO;
import cn.group.hotel2.pojo.entity.Clean;
import cn.group.hotel2.pojo.vo.*;
import cn.group.hotel2.service.ICleanService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 处理房间清洁数据相关的业务实现类
 *
 * @author wcl
 * @version 0.0.1
 * @date 2022/11/25
 */
@Slf4j
@Service
public class CleanServiceImpl implements ICleanService {

    @Autowired
    private CleanMapper cleanMapper;

    @Autowired
    private OrderMapper orderMapper;

    public CleanServiceImpl() {
        log.debug("创建业务类对象:CleanServiceImpl");
    }

    @Override
    public void addNew(CleanAddNewDTO cleanAddNewDTO) {
        log.debug("开始处理【新增清洁客房】的业务,参数:{}", cleanAddNewDTO);

        String roomTitle = cleanAddNewDTO.getRoomTitle();
        int count = cleanMapper.countByRoomTitle(roomTitle);
        if(count == 1) {
            String message = "新增客房失败,客房名已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        CheckOutStandardVO queryResult = orderMapper.getStandardByRoomTitle(roomTitle);
        if(queryResult == null) {
            String message = "新增客房失败,您填写的客房名称错误!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("根据客房名称查询退房时间,结果:{}", queryResult);
        LocalDateTime checkOutTime = queryResult.getCheckOutTime();
        Clean clean = new Clean();
        BeanUtils.copyProperties(cleanAddNewDTO,clean);
        clean.setCheckOutTime(checkOutTime);
        clean.setStatus(0);
        log.debug("即将向清洁表中插入数据,{}", clean);
        int rows = cleanMapper.insert(clean);
        if(rows != 1) {
            String message = "新增客房失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("新增客房完成!");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除客房清洁】的业务,参数:{}", id);

        CleanStandardVO queryResult = cleanMapper.getStandardById(id);
        log.debug("根据id【{}】查询客房详情,结果为:{}", id, queryResult);
        if (queryResult == null) {
            String message = "删除客房失败,该条数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将删除该条客房");
        int rows = cleanMapper.deleteById(id);
        if(rows != 1) {
            String message = "删除客房失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("删除客房完成!");
    }

    @Override
    public void updateCleanerAndStatusById(Long id, String username) {
        log.debug("开始处理【根据id修改客房清洁员和状态】的业务,参数:{},{}", id, username);
        CleanStandardVO queryResult = cleanMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "清洁客房失败,该客房详情不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Clean clean = new Clean();
        clean.setId(id);
        clean.setCleaner(username);
        clean.setStatus(1);
        log.debug("即将修改客房信息,{}", clean);
        int rows = cleanMapper.update(clean);
        if(rows != 1) {
            String message = "修改客房信息失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("修改客房信息完成!");
    }

    @Override
    public void updateStatusById(Long id) {
        log.debug("开始处理【根据id修改客房状态】的业务,参数:{}", id);

        CleanStandardVO queryResult = cleanMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "清洁客房失败,该客房详情不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Clean clean = new Clean();
        clean.setId(id);
        clean.setStatus(2);
        log.debug("即将修改客房状态,{}", clean);
        int rows = cleanMapper.update(clean);
        if(rows != 1) {
            String message = "修改客房信息失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("修改客房状态完成!");
    }

    @Override
    public CleanStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据客房id查询客房详情】的业务,参数:{}", id);

        CleanStandardVO queryResult = cleanMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询客房详情失败,该详情数据不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<CleanListItemVO> list() {
        log.debug("开始处理【查询清洁列表】的业务,无参数");

        return cleanMapper.list();
    }

    @Override
    public List<CleanListItemVO> listByPageNum(Long pageNum) {
        log.debug("开始处理【根据所选的页数来分页查询】的业务,参数:{}", pageNum);
        return cleanMapper.listByPageNum(pageNum);
    }

    @Override
    public List<CleanListCountVO> listCount() {
        return cleanMapper.listCount();
    }

    @Override
    public List<CleanListItemVO> listWaitClean() {
        log.debug("开始处理【查询处于待清洁状态下的清洁列表】的业务,无参数");
        return cleanMapper.listWaitClean();
    }

    @Override
    public List<CleanListItemVO> listCurrentClean() {
        log.debug("开始处理【查询处于正在清洁状态下的清洁列表】的业务,无参数");
        return cleanMapper.listCurrentClean();
    }

    @Override
    public List<CleanListItemVO> listFinishClean() {
        log.debug("开始处理【查询处于完成清洁状态下的清洁列表】的业务,无参数");
        return cleanMapper.listFinishClean();
    }

    @Override
    public List<CleanListItemVO> listWaitCleanByPageNum(Long pageNum) {
        log.debug("开始处理【根据所选的页数来分页查询】的业务,参数:{}", pageNum);
        return cleanMapper.listWaitCleanByPageNum(pageNum);
    }

    @Override
    public List<CleanListCountVO> listWaitCleanCount() {
        log.debug("开始处理【查询列表所有的数据以统计其数量】业务,无参数");
        return cleanMapper.listWaitCleanCount();
    }

    @Override
    public List<CleanListItemVO> listCurrentCleanByPageNum(Long pageNum) {
        log.debug("开始处理【根据所选的页数来分页查询】的业务,无参数");
        return cleanMapper.listCurrentCleanByPageNum(pageNum);
    }

    @Override
    public List<CleanListCountVO> listCurrentCleanCount() {
        log.debug("开始处理【查询列表所有的数据以统计其数量】的业务,无参数");
        return cleanMapper.listCurrentCleanCount();
    }

    @Override
    public List<CleanListItemVO> listFinishCleanByPageNum(Long pageNum) {
        log.debug("开始处理【根据所选的页数来分页查询】的业务,无参数");
        return cleanMapper.listFinishCleanByPageNum(pageNum);
    }

    @Override
    public List<CleanListCountVO> listFinishCleanCount() {
        log.debug("开始处理【查询列表所有的数据以统计其数量】的业务,无参数");
        return cleanMapper.listFinishCleanCount();
    }

}
